import {
  createStore,
  combineReducers,
  applyMiddleware,
} from 'redux';
import thunkMiddleware from 'redux-thunk';
import {
  syncHistoryWithStore,
  routerMiddleware,
  routerReducer,
} from 'react-router-redux';
import { createLogger } from 'redux-logger';
import reducers from '../containers/Reducers';

export default function configureStore(routerHistory) {
  // Add the reducer to your store on the `routing` key
  const allReducers = combineReducers({ ...reducers, routing: routerReducer });
  // Can add additional arguments to async action creator
  // https://github.com/gaearon/redux-thunk#injecting-a-custom-argument
  const middlewares = [thunkMiddleware, routerMiddleware(routerHistory)];
  // Can implement it manually, http://redux.js.org/docs/advanced/Middleware.html#the-final-approach
  if (['prod', 'production', 'staging'].indexOf(APP_CONFIG.env) < 0) {
    middlewares.push(createLogger());
  }
  // Can set preload state here if needed
  const store = createStore(allReducers, applyMiddleware(...middlewares));

  // Create an enhanced history that syncs navigation events with the store
  const storeHistory = syncHistoryWithStore(routerHistory, store);

  return [store, storeHistory];
}
